package com.qualitair44.activity;

import com.qualitair44.R;
import com.qualitair44.constantes.Constantes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class QuizzActivity extends Activity implements OnClickListener {

	private Button myButton;
	int numQuestion;
	int score;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.quizz_layout);

		Intent intent = getIntent();		
		numQuestion = intent.getIntExtra("numQuestion", 0);
		score = intent.getIntExtra("score", 0);

		TextView questionTV = (TextView) findViewById(R.id.quizzQuestion);
		questionTV.setText(Constantes.questionsQuizz[numQuestion]);

		RadioButton rb = (RadioButton) findViewById(R.id.radioQuizzRep1);
		rb.setText(Constantes.answersQuizz[numQuestion][0]);
		rb = (RadioButton) findViewById(R.id.radioQuizzRep2);
		rb.setText(Constantes.answersQuizz[numQuestion][1]);
		rb = (RadioButton) findViewById(R.id.radioQuizzRep3);
		rb.setText(Constantes.answersQuizz[numQuestion][2]);

		myButton = (Button) findViewById(R.id.buttonQuizzNextQuestion);		
		myButton.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {

		if(v == myButton) {	
			if(numQuestion<Constantes.correctAnswerQuizz.length)
			{
				if(getIndexSelectedAnswer() == Constantes.correctAnswerQuizz[numQuestion]){
					score++;
				}			
				numQuestion++;			

				//Intent intent = new Intent(this, QuizzActivity.class);
				Intent intent = new Intent(this, QualitAir44Activity.class);
				intent.putExtra("numQuestion", numQuestion);
				intent.putExtra("score", score);

				startActivity(intent); 
			}
			else {
				// show score
				Intent intent = new Intent(this, QuizzResultatActivity.class);
				intent.putExtra("numQuestion", 0);
				intent.putExtra("score", score);

				startActivity(intent); 
			}
		}


	}


	private int getIndexSelectedAnswer() {		
		RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroupQuizz);

		switch (rg.getCheckedRadioButtonId()) {
		case R.id.radioQuizzRep1:
			return 1;
		case R.id.radioQuizzRep2:
			return 2;
		case R.id.radioQuizzRep3:
			return 3;
		default:
			return 0;
		}
	}

}
